import container from './container'
const INJECTION = Symbol.for("INJECTION");

export default function Inject(serviceIdentifier: string) {
  return function (proto: any, key: string) {
    let resolve = () => {
      // 从 container 中取值
      return container.get(serviceIdentifier);
    };

    function getter() {
      // 缓存值
      if (!Reflect.hasMetadata(INJECTION, this, key)) {
        Reflect.defineMetadata(INJECTION, resolve(), this, key);
      }
      if (Reflect.hasMetadata(INJECTION, this, key)) {
        return Reflect.getMetadata(INJECTION, this, key);
      } else {
        return resolve();
      }
    }

    function setter(newVal: any) {
      Reflect.defineMetadata(INJECTION, newVal, this, key);
    }

    Object.defineProperty(proto, key, {
      configurable: true,
      enumerable: true,
      get: getter,
      set: setter
    });
  };
}
